from django import template
import re
import cgi
import string

from wiki.models import Page,File,Category

from markdown import markdown

register = template.Library()


# register the templatetags
@register.filter("wikitext") 
def wikitext(content):
	return markdown(content,["wikilink"])



class SidebarNode(template.Node):
	def render(self, context):
		try:
			str="<ul>"
			pages = Page.objects.exclude(slug="Wiki-Sidebar").order_by('title')
			for page in pages:
				str += "<li><a href=\""+page.get_absolute_url()+"\">"+page.title+"</a></li>" 
				
			str += "</ul>"
			#str = wiki_text(Page.objects.get(slug="Wiki-Sidebar").current_text.text)
		except:
			str =""
		return str
		
@register.tag
def wiki_sidebar(parser, token):
	return SidebarNode()
